<template>
  <el-form ref="formRef" :model="form" label-width="120px" size="default">
    <el-form-item>
      <h2>
        <el-link type="warning">check表未绑管，先绑管再推阴性，一次性执行。</el-link>
        <br />
      </h2>
    </el-form-item>
    <el-form-item label="试管号：">
      <el-input v-model="form.number" clearable class="input-width"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="add">添加</el-button>
      <el-button type="danger" @click="reset">重置</el-button>
    </el-form-item>
    <el-form-item label="">
      {{ JSON.stringify(form.numbers) }}
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">生成脚本</el-button>
      <el-button type="default" @click="exportFile">导出文件</el-button>
    </el-form-item>
    <el-form-item label="脚本：">
      <el-input autosize type="textarea" v-model="form.scriptString" readonly></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, reactive } from "vue";
import { format } from "./js/timeFromat";
import saveAs from "@/views/script-generator/js/FileSaver";
// do not use same name with ref
const form = reactive({
  // 试管号
  number: "",
  numbers: [],
  // 脚本
  scriptString: ""
});

const add = () => {
  form.number.trim() ? form.numbers.push(form.number.trim()) : "";
  form.number = "";
};

const reset = () => {
  form.number = "";
  form.numbers = [];
  form.scriptString = "";
};

const exportFile = () => {
  let str = new Blob([form.scriptString], {
    type: "text/plain;charset=utf-8"
  });
  saveAs(str, `先绑定check表中的试管，再推阴性脚本.txt`);
};

const onSubmit = () => {
  form.scriptString = `环境：太极政务云
数据库地址：192.159.107.33:2000
数据库名：nat_admin
涉及数据量：nat_appo_check 1条
nat_appo_check_relation 1条

操作类型：修改

先备份：
var c = db.nat_appo_check_relation.find({"tube_code":{"$in":${JSON.stringify(form.numbers)}}})
d = []
while(c.hasNext()) {
d.push(c.next())
}
printjson(d)


后修复：
db.nat_appo_check_relation.find({"tube_code":{"$in":${JSON.stringify(form.numbers)}}}).forEach(function(document){
if(document.appo_status != 6){
var checkObj = db.nat_appo_check.findOne({"id":document.id});
if(checkObj != null){
db.nat_appo_check.update({"id":document.id},
{$set:{"sam_time": document.sam_time,"tube_id":document.tube_id,"tube_code":document.tube_code, "update_name":"admin"}});

db.nat_tube.find({$and:[{"tube_code":{"$in":[document.tube_code]}}]}
).forEach(function (tubeObj) {
var appoCheckList = db.getCollection('nat_appo_check').find({"tube_id": tubeObj.id}).
forEach( function ( appoCheckObj) {
db.getCollection('nat_appo_check').update({"tube_id":tubeObj.id,id:appoCheckObj.id},{$set:{
status : NumberInt(6),
check_result_time:tubeObj.audit_time,
modify_time: tubeObj.audit_time,
audit_time : tubeObj.audit_time,
completed_time : tubeObj.completed_time,
abnormal_sign : NumberInt(0),
check_result : NumberInt(0)}},false,true);
var relationObj = db.getCollection('nat_appo_check_relation').findOne({"appo_check_id": appoCheckObj.id});
if(relationObj != null){
relationObj.appo_status = NumberInt(6);
relationObj.check_result = NumberInt(0);
relationObj.check_result_time = tubeObj.audit_time;
relationObj.update_user_name = 'sys';
relationObj.auditer = tubeObj.auditer;
relationObj.auditer_account = tubeObj.auditer_account;
relationObj.modify_time = tubeObj.modify_time;
relationObj.audit_time = tubeObj.audit_time;
relationObj.completed_time = tubeObj.completed_time;
relationObj.sam_inst_id = tubeObj.sam_inst_id;
relationObj.check_inst_id = tubeObj.check_inst_id;
relationObj.check_date = tubeObj.check_time.substring(0, 10);
relationObj.check_time = tubeObj.check_time;
relationObj.sam_dev_code = tubeObj.sam_dev_code;
relationObj.abnormal_sign = NumberInt(0);
relationObj.belongto_checkinst_id = tubeObj.belongto_checkinst_id;
relationObj.check_user_name = tubeObj.check_user_name;
if (appoCheckObj.sam_id != null) {
var samPointInfo = db.getCollection('nat_inst_sample_point').findOne({"id": appoCheckObj.sam_id});
if (samPointInfo != null) {
if (samPointInfo.sample_point_name != null) {
relationObj.sample_point_name = samPointInfo.sample_point_name
}
if (samPointInfo.sample_point_type != null) {
relationObj.sample_point_type = samPointInfo.sample_point_type;
}
}
}
if (tubeObj.sam_inst_id != null) {
var samInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.sam_inst_id});
if (samInstInfo != null) {
if (samInstInfo.inst_name != null) {
relationObj.sam_inst_name = samInstInfo.inst_name;
}
}
}
if (tubeObj.check_inst_id != null) {
var checkInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.check_inst_id});
if (checkInstInfo != null) {
if (checkInstInfo.inst_name != null) {
relationObj.check_inst_name = checkInstInfo.inst_name;
}
}
}
relationObj.completed_time = tubeObj.completed_time;
relationObj.check_result_sync_flag = NumberInt(1);
relationObj.report_path = "/opt/azero/nat-gen-report/data/" + appoCheckObj.inst_id + "/" + tubeObj.audit_time.substring(0, 10) + "/" + appoCheckObj.checked_person_card_number + ".pdf";
db.getCollection('nat_appo_check_relation').save(relationObj);
}else{
print(appoCheckObj.id+'无中间表信息');
var relationInfo = new Object();
if (appoCheckObj.id != null) {
relationInfo.appo_check_id = appoCheckObj.id;
}
if (appoCheckObj.tube_id != null) {
relationInfo.tube_id = appoCheckObj.tube_id;
}
if (appoCheckObj.checked_person_name != null) {
relationInfo.checked_person_name = appoCheckObj.checked_person_name;
}
if (appoCheckObj.tube_code != null) {
relationInfo.tube_code = appoCheckObj.tube_code;
}
if (appoCheckObj.sam_id != null) {
relationInfo.samp_id = appoCheckObj.sam_id;
}
if (appoCheckObj.status != null) {
relationInfo.appo_status = appoCheckObj.status;
}
if (appoCheckObj.sam_time != null) {
relationInfo.sam_time = appoCheckObj.sam_time;
}
if (appoCheckObj.appo_type != null) {
relationInfo.check_type = appoCheckObj.appo_type;
}
if (appoCheckObj.card_number_type != null) {
relationInfo.card_number_type = appoCheckObj.card_number_type;
}
if (appoCheckObj.checked_person_card_number != null) {
relationInfo.checked_person_card_number = appoCheckObj.checked_person_card_number;
}
if (appoCheckObj.checked_person_telephone != null) {
relationInfo.checked_person_telephone = appoCheckObj.checked_person_telephone;
}
if (appoCheckObj.checked_person_live_address != null) {
relationInfo.checked_person_live_address = appoCheckObj.checked_person_live_address;
}
if (appoCheckObj.checked_person_detail_address != null) {
relationInfo.checked_person_detail_address = appoCheckObj.checked_person_detail_address;
}
if (appoCheckObj.payment_fees != null) {
relationInfo.payment_fees = appoCheckObj.payment_fees;
}
if (appoCheckObj.payment_time != null) {
relationInfo.payment_time = appoCheckObj.payment_time;
}
if (appoCheckObj.sam_id != null) {
var samPointInfo = db.getCollection('nat_inst_sample_point').findOne({"id": appoCheckObj.sam_id});
if (samPointInfo != null) {
if (samPointInfo.sample_point_name != null) {
relationInfo.sample_point_name = samPointInfo.sample_point_name
}
if (samPointInfo.sample_point_type != null) {
relationInfo.sample_point_type = samPointInfo.sample_point_type;
}
}
}
if (tubeObj.sam_inst_id != null) {
relationInfo.sam_inst_id = tubeObj.sam_inst_id;
}
if (tubeObj.sam_inst_id != null) {
var samInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.sam_inst_id});
if (samInstInfo != null) {
if (samInstInfo.inst_name != null) {
relationInfo.sam_inst_name = samInstInfo.inst_name;
}
}
}
if (tubeObj.check_inst_id != null) {
relationInfo.check_inst_id = tubeObj.check_inst_id;
}
if (tubeObj.check_inst_id != null) {
var checkInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.check_inst_id});
if (checkInstInfo != null) {
if (checkInstInfo.inst_name != null) {
relationInfo.check_inst_name = checkInstInfo.inst_name;
}
}
}
if (tubeObj.sam_dev_code != null) {
relationInfo.sam_dev_code = tubeObj.sam_dev_code;
}
if (tubeObj.belongto_checkinst_id != null) {
relationInfo.belongto_checkinst_id = tubeObj.belongto_checkinst_id;
}
if (tubeObj.check_time != null && tubeObj.check_time.length >= 10) {
relationInfo.check_date = tubeObj.check_time.substring(0, 10);
}
if (tubeObj.check_time != null) {
relationInfo.check_time = tubeObj.check_time;
}
if (tubeObj.check_result_time != null) {
relationInfo.check_result_time = tubeObj.check_result_time;
}
if (tubeObj.check_user_name != null) {
relationInfo.check_user_name = tubeObj.check_user_name;
}
if (tubeObj.check_result != null) {
relationInfo.check_result = tubeObj.check_result;
}
relationInfo.check_result_sync_flag = NumberInt(1);
relationInfo.report_path = "/opt/azero/nat-gen-report/data/" +
appoCheckObj.inst_id + "/" + tubeObj.audit_time.substring(0, 10) + "/" +
appoCheckObj.checked_person_card_number + ".pdf";
db.getCollection('nat_appo_check_relation').save(relationInfo);
}
});
});
}else{
print("没有绑定成功的身份证号："+document.checked_person_card_number)
}
}
})`;
};
</script>

<style>
.input-width {
  width: 500px;
}
</style>
